import dayjs from 'dayjs';
import 'dayjs/locale/zh-cn';

export function setupDayjs() {
  dayjs.locale('zh-cn');
}
export const defaultDateFormatLong = 'YYYY-MM-DDTHH:mm:ss';
export const defaultDateFormat = 'YYYY-MM-DD HH:mm:ss';
export const defaultDateFormatShort = 'YYYY-MM-DD';
export const RangeValueFormat = 'YYYY-MM-DDTHH:mm:ss';
export const RangeTimeDefault = [
  dayjs('00:00:00', 'HH:mm:ss'),
  dayjs('11:59:59', 'HH:mm:ss'),
];

/**
 * @param date 
 * @param template - format as follows:
 * 
 * 
 * - long: 'YYYY-MM-DDTHH:mm:ss'
 * 
 * - default: 'YYYY-MM-DD HH:mm:ss'
 * 
 * - short: 'YYYY-MM-DD'
 * 
 * - rangeValue: 'YYYY-MM-DDTHH:mm:ss'
 * @returns 
 */
export function dateFormat(date?: dayjs.ConfigType, template?: "long" | "default" | "short" | "rangeValue" | (string & {})) {
  if (!template || template === "default") {
    template = defaultDateFormat
  } else if (template === "long") {
    template = defaultDateFormatLong
  } else if (template === "rangeValue") {
    template = RangeValueFormat
  } else if (template === "short") {
    template = defaultDateFormatShort
  }
  return dayjs(date).format(template);
}

export const isBeforeNow = (aDate: Date) => {
  const toDate = dayjs(aDate);
  return toDate.isBefore(dayjs());
};

export const startOfDay = (aDate?: dayjs.ConfigType) => {
  return dayjs(aDate).startOf('d');
};

export const endOfDay = (aDate?: dayjs.ConfigType) => {
  return dayjs(aDate).endOf('d');
};

export const diffBetween = (inp: dayjs.ConfigType, unitOfDiff: dayjs.OpUnitType = 'days', compare: dayjs.ConfigType = new Date()) => {
  return dayjs(inp).diff(dayjs(compare), unitOfDiff, false);
};

/**
 * 相对时间， eg '3天前', '5年后'
 * @param inp
 * @returns
 */
export const diffBetweenBetterShow = (inp: dayjs.ConfigType) => {
  const days = diffBetween(inp);
  if (days === 0) {
    return '今天' + dayjs(inp).format('HH:mm');
  } else {
    const num = Math.abs(days);
    const years = Math.floor(num / 365);
    let unit = '天';
    if (years > 0) {
      unit = '年';
    }
    if (days < 0) {
      return days + unit + '前';
    } else {
      return days + unit + '后';
    }
  }
};

const unitMap = {
  second: 1000,
  minute: 60 * 1000,
  hour: 60 * 60 * 1000,
};
/**
 * Get diff between two dates
 */
export function getDateDiff(date1: dayjs.ConfigType, date2: dayjs.ConfigType = new Date()): {value: {seconds:number;minutes:number;hours:number};expired:boolean} {
  const ret = {
    seconds: 0,
    minutes: 0,
    hours: 0,
  };
  let diff = dayjs(date1).diff(dayjs(date2));
  let expired = false;
  if (diff > 0) {
    ret.hours = Math.floor(diff / unitMap.hour);
    diff = diff - ret.hours * unitMap.hour;
    ret.minutes = Math.floor(diff / unitMap.minute);
    diff = diff - ret.minutes * unitMap.minute;
    ret.seconds = Math.floor(diff / unitMap.second);
  } else {
    expired = true;
  }
  return {
    value: ret,
    expired,
  };
}

export const longWeeks = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"]

export type Dayjs = dayjs.Dayjs;

/** 日期范围选择值
 * @description 用作antd RangePicker 组件 value 类型
 */
export type RangeValue<DateType = Dayjs> = [INullable<DateType> | undefined, INullable<DateType> | undefined];

export {
  dayjs as dateUtil
}